# coding:utf-8

from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
from flask_wtf import CSRFProtect
import redis


app = Flask(__name__)


class Config(object):
    """配置信息"""
    SECRET_KEY = "clown"
    # 数据库
    SQLALCHEMY_DATABASE_URI = "mysql://root:990518@127.0.0.1:3306/ihome"
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SQLALCHEMY_ECHO = True
    # redis
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = "6379"

    # flask-session配置
    SESSION_TYPE = "redis"
    SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)
    SESSION_USE_SIGNER = True  # 对cookie中的session_id进行隐藏
    PERMANENT_SESSION_LIFTTIME = 604800  # session数据的有效期,单位:秒


# class DevelopmentConfig(Config):
#     """开发模式配置信息"""
#     DEBUG = True
#
#
# class ProductConfig(Config):
#     """生产环境配置信息"""
#     pass
#
#
# config_dict = {
#     "develop": DevelopmentConfig,
#     "product": ProductConfig
# }

app.config.from_object(Config)


# 工厂模式
# def create_app(config_name):
#     """
#     创建flask的应用对象
#     :param config_name: str  配置模式的名字    ("develop", "product")
#     :return:
#     """
    # app = Flask(__name__)

    # 根据配置模式的名字获取配置参数的类
    # config_class = config_dict.get(config_name)
    # app.config.from_object(config_class)

    # 使用app初始化db
    # db.init_app(app)

    # 初始化redis工具
    # global redis_store
    # redis_store = redis.StrictRedis(host=config_class.REDIS_HOST, port=config_class.REDIS_PORT)

    # 利用flask-session,将session数据保存到redis中
    # Session(app)

    # 为flask补充csrf防护
    # CSRFProtect(app)

    # 注册蓝图
    # app.register_blueprint(api_1_0.api, url_prefix="/api/v1.0")

    # return app


# 创建flask的应用对象
# app = create_app("develop")

# 数据库
db = SQLAlchemy(app)

# 利用flask-session,将session数据保存到redis中
Session(app)

# 为flask补充csrf防护
CSRFProtect(app)

manager = Manager(app)
Migrate(app, db)
manager.add_command("db", MigrateCommand)

if __name__ == '__main__':
    manager.run()
